package com.android.lovepick;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.cookie.Cookie;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.android.lovepick.util.BaseUtils;
import com.android.lovepick.util.Constant;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;

public class MainApp extends Application
{
	private final static String MAINAPP_TAG = "MainApp";
	public static MainApp mainApp;
	public BMapManager bmapMan = null;
	public static final String key = "368781BA0403027777DA682D63972CE066162A44";

	@Override
	public void onCreate()
	{
		Log.v(MAINAPP_TAG, "onCreate");
		mainApp = this;
		bmapMan = new BMapManager(this);
		bmapMan.init(MainApp.key, new MyGeneraListener());
		bmapMan.getLocationManager().setNotifyInternal(10, 5);

		if (mainApp.getCookie() != null)
		{
			Log.i(MAINAPP_TAG, mainApp.getCookie());
		}
		else
		{
			Log.i(MAINAPP_TAG, "mainApp.getCookie() null...");
		}

		super.onCreate();
	}

	@Override
	public void onTerminate()
	{
		if (bmapMan != null)
		{
			bmapMan.destroy();
			bmapMan = null;
		}
		super.onTerminate();
	}

	public Map<String, String> getLogin()
	{
		Map<String, String> loginMap = null;

		SharedPreferences loginPrefs = mainApp.getSharedPreferences(
				Constant.PREFS_LOGIN, Context.MODE_PRIVATE);
		if (loginPrefs != null)
		{
			if (loginPrefs.contains("username")
					&& loginPrefs.contains("password"))
			{
				loginMap = new HashMap<String, String>();
				loginMap.put("username", loginPrefs.getString("username", null));
				loginMap.put("password", loginPrefs.getString("password", null));
			}
		}

		return loginMap;
	}

	public void setLogin(Map<String, String> loginMap)
	{
		String username = loginMap.get("username");
		String password = loginMap.get("password");

		SharedPreferences loginPrefs = mainApp.getSharedPreferences(
				Constant.PREFS_LOGIN, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = loginPrefs.edit();
		editor.putString("username", username);
		editor.putString("password", password);
		editor.commit();
	}

	public String getCookie()
	{
		String cookie = null;
		SharedPreferences cookiePrefs = mainApp.getSharedPreferences(
				Constant.PREFS_COOKIE, Context.MODE_PRIVATE);
		if (cookiePrefs != null)
		{
			if (cookiePrefs.contains("memberID")
					&& cookiePrefs.contains("memberPassword"))
			{
				cookie = "memberID=";
				cookie += cookiePrefs.getString("memberID", null);
				cookie += ";memberPassword=";
				cookie += cookiePrefs.getString("memberPassword", null);
			}
		}
		return cookie;
	}

	public void setCookie(List<Cookie> cookies)
	{
		SharedPreferences cookiePrefs = mainApp.getSharedPreferences(
				Constant.PREFS_COOKIE, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = cookiePrefs.edit();
		if (!cookies.isEmpty())
		{
			for (Cookie cookie : cookies)
			{
				editor.putString(cookie.getName(), cookie.getValue());
			}
			editor.commit();
		}
	}

	// public User getUserInfo()
	// {
	// User user = null;
	// SharedPreferences userPrefs = mainApp.getSharedPreferences(
	// Constant.PREFS_USER, Context.MODE_PRIVATE);
	// if (userPrefs != null)
	// {
	// if (userPrefs.contains("ID"))
	// {
	// user = new User();
	// user.setID(userPrefs.getString("ID", null));
	// }
	// }
	//
	// return user;
	//
	// }

	public String getUserName()
	{
		String username = null;
		SharedPreferences userPrefs = mainApp.getSharedPreferences(
				Constant.PREFS_LOGIN, Context.MODE_PRIVATE);
		if (userPrefs != null)
		{
			if (userPrefs.contains("username"))
			{
				username = userPrefs.getString("username", null);
			}
		}
		return username;
	}

	public static class MyGeneraListener implements MKGeneralListener
	{

		public void onGetNetworkState(int error)
		{
			// TODO Auto-generated method stub
			Log.i(MAINAPP_TAG, "MyGeneralListener:onGetNetworkState error is "
					+ error);
			BaseUtils.showToast(mainApp.getApplicationContext(),
					R.string.net_error);
		}

		public void onGetPermissionState(int error)
		{
			// TODO Auto-generated method stub

		}

	}

}
